package com.ssm.blog.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.blog.pojo.Diary;
import com.ssm.blog.pojo.Role;
import com.ssm.blog.pojo.criteria.DiaryCriteria;
import com.ssm.blog.service.DiaryService;
import com.ssm.blog.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/diary.html")
public class DiaryController {
    @Autowired
    private DiaryService diaryService;

    @RequestMapping(params = "act=list")
    @ResponseBody
    public Map<String, Object> diaryTable(DiaryCriteria criteria, Integer page, Integer rows) {
        PageInfo<Diary> pageInfo = diaryService.getDiaryList(criteria, page, rows);
        //key不可以改，页面上分页插件需要
        return JsonUtil.getDatagridPagerResult(pageInfo.getTotal(), pageInfo.getList());
    }

    @RequestMapping(params = "act=add")
    @ResponseBody
    public Map<String, Object> add(Diary diary) {
        diary.setUserId(1);
        diaryService.addDiary(diary);
        return JsonUtil.getSuccessMessage("添加成功", null);
    }

    @RequestMapping(params = "act=delete")
    @ResponseBody
    public Map<String, Object> delete(Integer id) {
        diaryService.deleteDiary(id);
        return JsonUtil.getSuccessMessage("删除成功", null);
    }
}
